package com.salmon.oss.config;

import com.salmon.oss.core.usermgr.UserService;
import com.salmon.oss.security.OperationAccessControl;
import com.salmon.oss.server.BucketService;
import com.salmon.oss.server.ThriftApiServer;
import com.salmon.oss.server.store.OssStoreService;
import com.salmon.oss.service.OssHandlerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ThriftConfig {

    @Autowired
    private BucketService bucketService;

    @Autowired
    private OssStoreService ossStoreService;

    @Autowired
    private OperationAccessControl operationAccessControl;

    @Autowired
    private UserService userService;

    @Bean
    public OssHandlerServiceImpl ossHandlerService() {
        OssHandlerServiceImpl ossHandlerService = new OssHandlerServiceImpl();
        ossHandlerService.setBucketService(bucketService);
        ossHandlerService.setOssStoreService(ossStoreService);
        ossHandlerService.setOperationAccessControl(operationAccessControl);
        ossHandlerService.setUserService(userService);
        return ossHandlerService;
    }

    @Bean
    @ConditionalOnMissingBean(name = "thriftApiServer")
    public ThriftApiServer thriftApiServer(@Autowired OssHandlerServiceImpl ossHandlerService,
                                           @Value("${thrift.port}") Integer port) {
        ThriftApiServer thriftApiServer = new ThriftApiServer(ossHandlerService);
        if(port != null)
            thriftApiServer.setPort(port);
        return thriftApiServer;
    }
}
